package com.team2.outpatient_sys.web.pharmacyControllers;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.pojo.*;
import com.team2.outpatient_sys.service.InventoryService;
import com.team2.outpatient_sys.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 实现对药品的库存和过期时间进行预警
 * 每次操作一次药品库存表调用查询药品库存方法一次，
 * 处理临期药品，修改该药品的数量和信息，把该条数据加入不合格药品表中
 *
 * */
@RestController
@RequestMapping("/inventorys")
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    //查询库房所有的药品
    @GetMapping("/findAllPharmacyStorage/{currentPage}/{page}")
    public Result findAllPharmacyStorage(@PathVariable Integer currentPage,@PathVariable Integer page){
        PageHelper.startPage(currentPage, page);
        Result result = new Result();
        List<PharmacyStorage> all = inventoryService.findAllPharmacyStorage();
        PageInfo<PharmacyStorage> pharmacyStorageList = new PageInfo<>(all);
        if (pharmacyStorageList == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else{
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pharmacyStorageList);
        }
        return result;
    }
    //药品库存不足提示
    @GetMapping("/reminderUnderstock")
    public Result reminderUnderstock(){
        Result result = new Result();
        List<PharmacyStorage> ps = inventoryService.reminderUnderstock();
        result.setCode(200);
        if (ps.isEmpty()){
            result.setMsg("药品库存正常");
        }else{
            result.setMsg("药品库存不足");
            result.setData(ps);

        }
        return result;
    }

    //药品临期提醒
    @GetMapping("/deadlineRemind")
    public Result deadlineRemind(){
        Result result = new Result();
        List<PharmacyStorage> dr = inventoryService.deadlineRemind();
        result.setCode(200);
        if (dr.isEmpty()){
            result.setMsg("药品正常");
        }else{
            result.setMsg("药品临期");
            result.setData(dr);
        }
        return result;
    }

    //处理临期药品,添加信息到不合格药品表
    @PostMapping("/issue")
    public Result addIssue(@RequestBody UnqualifiedMedicine unqualifiedMedicine){
        System.out.println(unqualifiedMedicine);
        unqualifiedMedicine.setRecordTime(DateUtils.getDate());
        Result result = new Result();
        Integer add = inventoryService.addUnqualifiedMedicine(unqualifiedMedicine);
        if (add < 1){
            result.setCode(500);
            result.setMsg("添加失败");
        }else{
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }

    //药品退回库房未完成展示
    @GetMapping("/storageOutNO/{currentPage}/{page}")
    public Result storageOutNO(@PathVariable Integer currentPage,@PathVariable Integer page){
        Result result = new Result();
        PageHelper.startPage(currentPage, page);
        List<ShowUnqualifiedMedicine> all = inventoryService.storageOutNO();
        PageInfo<ShowUnqualifiedMedicine> unqualifiedMedicineList = new PageInfo<>(all);
        if (unqualifiedMedicineList != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(unqualifiedMedicineList);
        }else{
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    //药品退回之前的信息修改
    @PutMapping("/updateError")
    public Result updateError(@RequestBody UnqualifiedMedicine unqualifiedMedicine){
        Result result = new Result();
        Integer updateStorageOut = inventoryService.updateError(unqualifiedMedicine);
        if(updateStorageOut<1){
            result.setMsg("操作失败");
            result.setCode(500);
        }else {
            result.setCode(200);
            result.setMsg("操作成功");
            result.setData(updateStorageOut);
        }
        return result;
    }

    //药品退回库房操作
    @PutMapping("/UpdateStorageOut")
    public Result UpdateStorageOut(@RequestBody UnqualifiedMedicine unqualifiedMedicine){
        Result result = new Result();
        Integer updateStorageOut = inventoryService.UpdateStorageOut(unqualifiedMedicine);
        if(updateStorageOut<1){
            result.setMsg("操作失败");
            result.setCode(500);
        }else {
            result.setCode(200);
            result.setMsg("操作成功");
            result.setData(updateStorageOut);
        }
        return result;
    }

    //药品退回库房已完成
    @GetMapping("/storageOutYES/{currentPage}/{page}")
    public Result storageOutYES(@PathVariable Integer currentPage,@PathVariable Integer page){
        Result result = new Result();
        PageHelper.startPage(currentPage, page);
        List<ShowUnqualifiedMedicine> all = inventoryService.storageOutYES();
        PageInfo<ShowUnqualifiedMedicine> unqualifiedMedicineList = new PageInfo<>(all);
        if (unqualifiedMedicineList != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(unqualifiedMedicineList);
        }else{
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    //添加需要补充的药品信息和数量
    @PostMapping("/addSupplement")
    public Result add(@RequestBody Supplement supplement){
        Result result = new Result();
        Integer addSupplement = inventoryService.addSupplement(supplement);
        if (addSupplement < 1){
            result.setCode(500);
            result.setMsg("添加失败");
        }else{
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }

    //药品补充未完成
    @GetMapping("/showSupplement/{currentPage}/{page}")
    public Result showSupplement(@PathVariable Integer currentPage,@PathVariable Integer page){
        Result result = new Result();
        PageHelper.startPage(currentPage, page);
        List<Supplement> all = inventoryService.findAllSupplement();
        PageInfo<Supplement> supplement = new PageInfo<>(all);
        if (supplement != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(supplement);
        }else{
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    //修改申请的数量和其他的内容
    @PutMapping("/update")
    public Result update(@RequestBody Supplement supplement){
        Result result = new Result();
        Integer supplementList = inventoryService.update(supplement);
        if (supplementList >= 1){
            result.setCode(200);
            result.setMsg("修改成功");
        }else{
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }

    //药品补充申请操作
    @PutMapping("/updateSupplement")
    public Result updateSupplement(@RequestBody Supplement supplement){
        Result result = new Result();
        Integer supplementList = inventoryService.updateSupplement(supplement);
        if (supplementList >= 1){
            result.setCode(200);
            result.setMsg("操作成功");
        }else{
            result.setCode(500);
            result.setMsg("操作失败");
        }
        return result;
    }
}
